#include "ProcessPool.hpp"

int main()
{
    //创建进程池
    ProcessPool pp(7);

    //初始化进程池
    pp.Init_ProcessPool([](int fd){
        while(true)
        {
            int code = 0;
            //std::cout << "子进程被阻塞: " << getpid() <<std::endl;
            ssize_t n = read(fd, &code, sizeof(code));
            if(n == sizeof(code))
            {
                std::cout << "子进程被唤醒: " << getpid() << ", fd: " << fd <<std::endl;
                if(code >= 0 && code < tasks.size())
                {
                    tasks[code]();
                }
                else
                {
                    std::cerr << "父进程给出任务错误" << code << std::endl;
                }
            }
            else if(n == 0)
            {
                std::cout << "子进程该退出了" << getpid() << std::endl;
                break;
            }
            else
            {
                std::cerr << "read fd: " << "error" << std::endl;
                break; 
            }
        } 
    });
    pp.ProcessPoolList();

    //控制进程池
    pp.PollingCtrlSubProcess(10);

    //回收进程池
    pp.WaitSubProcess();

    std::cout << "父进程控制子进程结束" <<std::endl;

    return 0;
}